package com.example.SBootDemo.controller.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.model.function.role;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.roleinterface;
import com.example.SBootDemo.service.Interface.function.userinterface;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	roleinterface rolevice;
	
		 //返回rolelist页面
		 @RequestMapping(value="/getroles", method=RequestMethod.GET)
		 public String getUsers(HttpServletRequest request,@RequestParam(value="pageinfo", defaultValue="0") int pageinfo) throws Myexception {
			 int flag=rolevice.getRoles(pageinfo,request);
			
		     return "/fun/roleModel";
		 }
	
	//跳转添加角色页面
		 @RequestMapping(value="/addRole", method=RequestMethod.GET)
		 public String toUser(HttpServletRequest request) {
			 rolevice.addpage(request);
		     return "/fun/update/roleUpdate";
		 }
		 //添加成功后返回rorlelist页面
		 @RequestMapping(value="/addUser", method=RequestMethod.POST)
		 public String addRole(@RequestBody role role) {
		     
		     return "/getUsers";
		 }
		 //删除角色
		 @RequestMapping(value="/deleteRole", method=RequestMethod.POST)
		 @ResponseBody
		 public String deleteUser(@RequestBody List<Integer> listid) throws Myexception{
			 
			 rolevice.deleteRole(listid);
		     String message = "删除成功";
		     ObjectMapper mapper = new ObjectMapper();
		   
			 try {
				return mapper.writeValueAsString(message);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		 }
		 //删除角色
		 @RequestMapping(value="/deleteRole", method=RequestMethod.GET)
		 public String deleteOneUser(int id) throws Myexception{
			 List<Integer> deleteId = new ArrayList<>();
			 deleteId.add(id);
			 rolevice.deleteRole(deleteId);
			 return "redirect:/role/getroles";
		 }
		 
		 
		 //返回修改角色信息页面
		 @RequestMapping(value="/updateRole", method=RequestMethod.GET)
		 public String toupdateUser(int id,HttpServletRequest request) throws Myexception {
			 rolevice.updatepage(request,id);
			 return "/fun/update/roleUpdate";
		 }
		 
		 @RequestMapping(value="/updateRole", method=RequestMethod.POST)
		 public String updateUser( role role,int[] ckid,HttpServletRequest request) throws Myexception {
			 rolevice.updateRole(role,ckid,request);
		     return "redirect:/role/getroles";
		 }
		 

		 
}
